{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "738074e0",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os \n",
    "\n",
    "\n",
    "from os.path import join, dirname, abspath,isdir\n",
    "\n",
    "\n",
    "from tqdm import tqdm\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "class BoosterJsonlExporter:\n",
    "\n",
    "\n",
    "    def __init__(self, root):\n",
    "        self.root = root\n",
    "\n",
    "        self.sample_paths =  [join(root,x)  for x in os.listdir(root) if isdir(join(root,x))]\n",
    "        \n",
    "        for sample_path in tqdm(self.sample_paths):\n",
    "\n",
    "            imgs,depth = self.load_sample(sample_path)\n",
    "\n",
    "            \n",
    "            break\n",
    "        self.tmp = ( imgs,depth)\n",
    "\n",
    "            \n",
    "        \n",
    "\n",
    "    def load_sample(self,path):\n",
    "        cv_file = cv2.FileStorage(join(path,'calib_00-02.xml'), cv2.FILE_STORAGE_READ)    \n",
    "        f_disp = cv_file.getNode(\"proj_matL\").mat()[0,0]\n",
    "        cx = cv_file.getNode(\"proj_matL\").mat()[0,2]\n",
    "        cy = cv_file.getNode(\"proj_matL\").mat()[1,2]\n",
    "        b_disp = float(cv_file.getNode(\"baselineLR\").real()) / 1000.\n",
    "        cv_file.release()\n",
    "\n",
    "\n",
    "        \n",
    "        # join(path,'mask_00.npy')\n",
    "        def load_one_side(img_path, depth_path):\n",
    "            \n",
    "            imgs = []\n",
    "            for img_name in [ join(img_path, x) for x in os.listdir(img_path) if x.endswith('.png')]:\n",
    "                rgb = cv2.cvtColor(cv2.imread(img_name).astype(np.uint8), cv2.COLOR_BGR2RGB)\n",
    "                imgs.append(rgb)\n",
    "\n",
    "            turn_to_depth = True\n",
    "            depth = read_d(depth_path, invert=turn_to_depth, f=f_disp, b=b_disp)\n",
    "            return imgs,depth\n",
    "        depth_path = join(path,'disp_00.npy')\n",
    "        img_path = join(path,'camera_00')\n",
    "        imgs,depth = load_one_side(img_path,depth_path)\n",
    "\n",
    "        #* ignore \n",
    "        if False:\n",
    "                \n",
    "            join(path,'camera_02')\n",
    "            join(path,'disp_02.npy')\n",
    "            # join(path,'mask_02.npy')\n",
    "            \n",
    "        \n",
    "\n",
    "        return imgs,depth\n",
    "    \n",
    "    \n",
    "\n",
    "import cv2\n",
    "import numpy as np \n",
    "\n",
    "from utils import *\n",
    "root = '/share/project/cwm/shaocong.xu/exp/Lotus/data/booster'\n",
    "exporter = BoosterJsonlExporter(root)\n",
    "imgs,depth  = exporter.tmp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "64cf26f2",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d5c0b904",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "97b61e30",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "0722468f",
   "metadata": {},
   "source": [
    "# DEMO\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "94fa3d79",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "demo_path ='/share/project/cwm/shaocong.xu/exp/Lotus/data/booster/Bathroom/camera_00' \n",
    "\n",
    "\n",
    "import sys \n",
    "sys.path.insert(0,'/share/project/cwm/shaocong.xu/exp/Lotus')\n",
    "from daniel_tools.img_utils import concat_images\n",
    "\n",
    "\n",
    "from os.path import exists\n",
    "demo_imgs = [join(demo_path, x) for x in os.listdir(demo_path) if exists(join(demo_path, x) )]\n",
    "concat_images(demo_imgs)\n",
    "\n",
    "\n",
    "     "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "89938fd2",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "21bbbba2",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "\n",
    "import cv2\n",
    "def alpha_blend(images, alphas):\n",
    "    \"\"\"将多张图像进行 alpha blend。\n",
    "    \n",
    "    Args:\n",
    "        images: List of images to blend (should be the same size).\n",
    "        alphas: List of alpha values for each image (should sum up to 1).\n",
    "        \n",
    "    Returns:\n",
    "        Blended image.\n",
    "    \"\"\"\n",
    "    # 确保图像和 alpha 值数量一致\n",
    "    if len(images) != len(alphas):\n",
    "        raise ValueError(\"Number of images must match number of alpha values.\")\n",
    "    \n",
    "    # 确保 alpha 值合计为 1\n",
    "    if not np.isclose(sum(alphas), 1.0):\n",
    "        raise ValueError(\"Alpha values must sum to 1.\")\n",
    "    \n",
    "    # 初始化混合图像为零\n",
    "    blended = np.zeros_like(images[0], dtype=np.float32)\n",
    "    \n",
    "    # 进行 alpha blending\n",
    "    for img, alpha in zip(images, alphas):\n",
    "        blended += img.astype(np.float32) * alpha\n",
    "    \n",
    "    # 确保输出是无符号8位整数\n",
    "    blended = np.clip(blended, 0, 255).astype(np.uint8)\n",
    "    \n",
    "    return blended\n",
    "\n",
    "\n",
    "\n",
    "import numpy as np\n",
    "# 读取图像\n",
    "img1 = cv2.imread('image1.png')\n",
    "img2 = cv2.imread('image2.png')\n",
    "img3 = cv2.imread('image3.png')\n",
    "\n",
    "\n",
    "imgs = [cv2.imread(x) for x in demo_imgs]\n",
    "# # 确保所有图像具有相同的尺寸\n",
    "# img1 = cv2.resize(img1, (800, 600))\n",
    "# img2 = cv2.resize(img2, (800, 600))\n",
    "# img3 = cv2.resize(img3, (800, 600))\n",
    "\n",
    "# 定义 alpha 值\n",
    "# alphas = [0.33, 0.33, 0.34]\n",
    "alphas = [0.73, 0.13, 0.14]\n",
    "blended_image = alpha_blend(imgs, alphas)\n",
    "# cv2.imwrite('blended_image.png', blended_image)\n",
    "from PIL import Image\n",
    "Image.fromarray(blended_image)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cdb964d2",
   "metadata": {},
   "outputs": [],
   "source": [
    "alphas = [0.13, 0.73, 0.14]\n",
    "blended_image = alpha_blend(imgs, alphas)\n",
    "# cv2.imwrite('blended_image.png', blended_image)\n",
    "from PIL import Image\n",
    "Image.fromarray(blended_image)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d3e228a6",
   "metadata": {},
   "outputs": [],
   "source": [
    "alphas = [0.13, 0.13, 0.74]\n",
    "blended_image = alpha_blend(imgs, alphas)\n",
    "# cv2.imwrite('blended_image.png', blended_image)\n",
    "from PIL import Image\n",
    "Image.fromarray(blended_image)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
